<?php   
    if ($_SERVER['REQUEST_METHOD'] == 'POST')
    {
        include_once('ns_include.php');
        
        //For creating a new user from admin menu
        if (isset($_POST["submit"]) && $_POST['submit'] == "Submit")
        {
            $result = ns_dao_user::UserbyUsername($_POST["Username"]);

            if (count($result) == 0)
            {
                $messageString = "Account created";
                $result = ns_dao_user::CreateNewUser($_POST['Username'], md5($_POST['Password']), $_POST['FirstName'], $_POST['LastName'], $_POST['Email'], $_POST['Phone']);
                if ($result != 1)
                {
                    $messageString = "Account creation failed";    
                }
            } else {
                //Can't does user
                $messageString = "Username already taken.";
            }
            header("location: manage_users.php");
        }
        
        //For updating the user's profile info (not avatar)
        if (isset($_POST["save"]) && $_POST["save"] == "Save")
        {            
            $messageString = "Profile Updated";
            $userData = ns_dao_user::UserByUsername($_POST['username']);
            $userData->FirstName($_POST['FirstName']);
            $userData->LastName($_POST['LastName']);
            $userData->Email($_POST['Email']);
            $userData->Phone($_POST['Phone']);
            
            if (isset($_POST['UserLevel']) && $_SESSION['userLevel'] >= 3)
            {
                $userData->UserLevel($_POST['UserLevel']);
            }
            
            $result = $userData->UpdateUserToDatabase();
                        
            if (!$result == true) 
            {
                $messageString = "Profile was not updated";
            }
        }
        
        //For updating the user's avatar
        if (isset($_POST["uploadAvatar"]) && $_POST["uploadAvatar"] == "Update Avatar")
        {
            $messageString = "Avatar updated";            
            $userData = ns_dao_user::UserByUsername($_POST['username']);
            $result = $userData->UpdateAvatarImage($_FILES["avatarFile"]);
            if (!$result)
            {
                $messageString = "Avatar was not updated";
            }
        }
        
        //Delete user
        if (isset($_POST["delete"]) && $_POST["delete"] == "Delete")
        {
            $result = ns_dao_user::DeleteUser($_POST['username']);
        }
    }
?>
